<?php
declare (strict_types = 1);

namespace app\adminapi\controller\system;

use adminmatrix\admin\BaseController;
use adminmatrix\admin\service\JsonService;
use app\adminapi\logic\admin\AdminLogic;
use app\adminapi\logic\system\PermissionLogic;
use app\adminapi\model\Admin\Admin;
use think\Request;
use hg\apidoc\annotation as Apidoc;
use app\common\annotate\permission;
use think\response\Json;

#[
    Apidoc\Title("管理员"),
    Apidoc\Group("system"),

]
class AdminController extends BaseController
{
    #[
        Apidoc\Title("获取管理员列表"),
        Apidoc\Query(ref: [\app\common\apidoc\Definitions::class , 'pagingParam'])
    ]
    public function lists() : Json
    {
        return $this->dataLists();
    }

    #[
        Apidoc\Title("获取账号信息"),
        permission
    ]
    public function myself(Request $request , JsonService $jsonService) : Json
    {
        return $jsonService->handle(function(Request $request , AdminLogic $adminLogic){
            return $adminLogic->mySelf($request->admin_id);
        });
    }

    public function save( Request $request , AdminLogic $admin) : Json
    {
        try {
            $result = $admin->save($request);
            return JsonService::success('创建管理员成功');
        } catch (\Exception $e)
        {
            return JsonService::fail($e->getMessage());
        }
    }

    public function update(Request $request , AdminLogic $admin) : Json
    {
        try {
            $request = $admin->update($request);
            return JsonService::success('编辑成功');

        } catch (\Exception $e)
        {
            return JsonService::fail($e->getMessage());

        }
    }

    public function delete(Request $request , AdminLogic $admin) : Json
    {
        try {
            $result = $admin->delete($request);
            return  JsonService::success("删除管理员成功");
        } catch (\Exception $e)
        {
            return  JsonService::fail($e->getMessage());
        }
    }

}
